package com.jws.battleship;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FriendListActivity extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_list);
		

		ArrayList<Friend> listofFriends = new ArrayList<Friend>();
		Drawable pic = getResources().getDrawable(R.drawable.original);
		
		for(int i = 0; i< 7; i++)
		{
			Friend ff = new Friend(); 
			ff.setLosses(i+3/3);
			ff.setWins(i+9*2/6);
			ff.setName("Johnny0"+i);
			ff.setPicture(pic);
			ff.setLastOnline(Calendar.getInstance().getTime());
			ff.setRank(i);
			
			listofFriends.add(ff);
		}
		
		ListView list = (ListView)findViewById(R.id.friend_list_list);
		list.setCacheColorHint(Color.TRANSPARENT);
		
		//Adding footer
		View footer = (View)getLayoutInflater().inflate(R.layout.friend_row_footer, null);
		list.addFooterView(footer, null, false);
		 
        // Getting adapter by passing xml data ArrayList
		FriendListAdapter adapter= new FriendListAdapter(this, R.layout.friend_row, listofFriends);
        list.setAdapter(adapter);
 
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
			}});
	}
	
	private class FriendListAdapter extends ArrayAdapter<Friend>
	{    
	    ArrayList<Friend> data = null;
	    SimpleDateFormat sdf = new SimpleDateFormat("dd. MMM yyyy HH:mm:ss ");
	    
	    public FriendListAdapter(Context context, int layoutResourceId, ArrayList<Friend> data) {
	        super(context, layoutResourceId, data);
	        this.data = data;
	    }
	    
	    private Drawable getMedal(Integer rank)
	    {
	    	switch(rank)
	    	{
	    	case 1:
	    		return getResources().getDrawable(R.drawable.medal1);
	    	case 2 :
	    		return getResources().getDrawable(R.drawable.medal2);
	    	case 3:
	    		return getResources().getDrawable(R.drawable.medal3);
	    	case 4:
	    		return getResources().getDrawable(R.drawable.medal4);
	    	case 5:
	    		return getResources().getDrawable(R.drawable.medal5);
	    	default:
	    		return getResources().getDrawable(R.drawable.medal1);
	    	}
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {

	    	View v = convertView;
	    	if (v == null) {
	        	LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = vi.inflate(R.layout.friend_row, null);
	        }
	    	
	        Friend f = data.get(position);
	        if(f != null)
	        {

	        	ImageView Fpic = (ImageView)v.findViewById(R.id.friend_row_profilepic);
	        	ImageView Fmedal = (ImageView)v.findViewById(R.id.friend_row_medal);
	        	
	        	TextView fName = (TextView)v.findViewById(R.id.friend_row_name);
	        	TextView fWinLoss = (TextView)v.findViewById(R.id.friend_row_win_loss);
	        	TextView fdate = (TextView)v.findViewById(R.id.friend_row_date);
	        	
		        	
	        	if(Fpic != null && fName != null && fWinLoss != null && fdate != null && Fmedal != null)
	        	{
	        		Fpic.setImageDrawable(f.getPicture());
	        		Fmedal.setImageDrawable(getMedal(f.getRank()));
	        		
	        		fName.setText(f.getName());
	        		fWinLoss.setText("Won: " + f.getWins().toString() + "; Lost: " + f.getLosses().toString());
	        		fdate.setText("Last online " + sdf.format(f.getLastOnline()));
	        	}
	        } 
	        
	        
	        return v;
	        
	    }
	}

}


